package com.web44.springmvc.config;

import com.web44.springmvc.converter.MyDateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/**
* @Title: SpringMvcConfig
* @Description:
* @Auther:
* @Version: 1.0
* @create 2023/8/23 16:33
*/
@Configuration
@ComponentScan("com.web44.springmvc.web")
@EnableWebMvc  //开启json数据类型自动转换s
// SpringMvcConfig 默认扩展接口
public class SpringMvcConfig implements WebMvcConfigurer {

    @Autowired
    private MyDateConverter myDateConverter;

    /**
     * 类型转换器
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(myDateConverter);
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setCache(false);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        registry.viewResolver(viewResolver);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 访问路径 和项目 路径之间的映射【本地】
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/images/**").addResourceLocations("/images/");
    }

    //    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedOrigins("*")
//                .allowedMethods("GET","POST","PUT","DELETE","OPTIONS")
//               // .allowedHeaders("*")
//                .allowCredentials(true)
//                .maxAge(3600);
//    }
}
